package com.cloud.mall.consume.controller;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.consume.domain.ErrorLog;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.log.mapper.MallErrorLogMapper;
import com.cloud.mall.log.mapper.SenderContactsMapper;
import com.cloud.mall.log.model.MallErrorLog;
import com.cloud.mall.log.model.MallErrorLogExample;
import com.cloud.mall.log.model.SenderContacts;
import com.cloud.mall.log.model.SenderContactsExample;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "错误日志服务接口")
@RestController
@RequestMapping("/log")
public class LogController {

    @Autowired
    private MallErrorLogMapper mallErrorLogMapper;
    @Autowired
    private SenderContactsMapper senderContactsMapper;

    @ApiOperation(value = "查询错误日志列表")
    @GetMapping("/list")
    BaseResponse<CommonPage<ErrorLog>> list(ErrorLog errorLog, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        MallErrorLogExample example = new MallErrorLogExample();
        MallErrorLogExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(errorLog.getClassName())) {
            criteria.andClassNameLike("%" + errorLog.getClassName() + "%");
            example.or(example.createCriteria().andClassNameLike("%" + errorLog.getClassName() + "%"));
        }
        if (!StringUtils.isEmpty(errorLog.getServerId())) {
            criteria.andServerIdLike("%" + errorLog.getServerId() + "%");
            example.or(example.createCriteria().andServerIdLike("%" + errorLog.getServerId() + "%"));
        }
        if (!StringUtils.isEmpty(errorLog.getIpaddr())) {
            criteria.andIpaddrLike("%" + errorLog.getIpaddr() + "%");
            example.or(example.createCriteria().andIpaddrLike("%" + errorLog.getIpaddr() + "%"));
        }
        List<MallErrorLog> mallErrorLogs = mallErrorLogMapper.selectByExample(example);
        List<ErrorLog> errorLogs= MallBeanUtils.copyDoToDto(mallErrorLogs,ErrorLog.class);
        return BaseResponse.setResultSuccess(CommonPage.restPage(errorLogs));
    }

    @ApiOperation(value = "查询错误日志列表")
    @GetMapping("/senderList")
    BaseResponse<CommonPage<SenderContacts>> list(String keyword, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        SenderContactsExample example = new SenderContactsExample();
        SenderContactsExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andWechatNameLike("%" + keyword + "%");
            example.or(example.createCriteria().andWechatNameLike("%" + keyword + "%"));
        }
        List<SenderContacts> senderContacts = senderContactsMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(senderContacts));
    }

    @ApiOperation("删除指定人员")
    @PostMapping("/delete/{id}")
    BaseResponse delete(@PathVariable int id){
        int count= senderContactsMapper.deleteByPrimaryKey(id);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @ApiOperation("添加指定人员")
    @PostMapping("/add")
    BaseResponse add(@RequestBody SenderContacts senderContacts){
        int count= senderContactsMapper.insert(senderContacts);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }


}
